package com.see.domain;

import java.math.BigDecimal;
import java.util.HashMap;
import com.see.exception.MessegeFeedException;
import com.see.servise.SEEServise;

public class OrderServise {
		
	public OrderServise() {}
	
	public static Order createOrderFromMessege(HashMap<String, String> paramMap, boolean checkproduct) throws MessegeFeedException{
		
		String userName = getStringFromMap(MessegeParser.MsgFields.USERNAME.name(),paramMap);
		String product  = getStringFromMap(MessegeParser.MsgFields.PRODUCT.name() ,paramMap);
		
		if (checkproduct && !SEEServise.getInstance().checkProductBookExist(product))
			throw new MessegeFeedException(Protocol.errormsg(
					Protocol.SENDORDED_FIELD, Protocol.ERROR_PRODUCT + MessegeParser.MsgFields.PRODUCT.name()));		
		
		String strAmount = getStringFromMap(MessegeParser.MsgFields.AMOUNT.name(), paramMap);
		long amount = Long.parseLong(strAmount);
		
		Operation type = getOperationFromMap(paramMap);
		
		Price prise = getPriseFromMap(paramMap);
		
		Order tmpOrder = new Order(userName, product, prise, amount, type);
		
		return tmpOrder;
	}
	
	public static Order createMatchOrderFromMessege(HashMap<String, String> paramMap) throws MessegeFeedException{
		Order tmpOrder = createOrderFromMessege(paramMap, false);
		
		String srtorderid = getStringFromMap(MessegeParser.MsgFields.ORDERID.name(),paramMap);
		tmpOrder.setId(Integer.parseInt(srtorderid));
		BigDecimal matchprice = new BigDecimal(getStringFromMap(MessegeParser.MsgFields.ORDERMATCHPRICE.name(), paramMap)); 
		tmpOrder.setMatchprice(matchprice);
		String counterparty = getStringFromMap(MessegeParser.MsgFields.ORDERCOUNTERPARTY.name(), paramMap);
		tmpOrder.setCounterparty(counterparty);
		
		return tmpOrder;
		
	}
	
	
	
	private static String getStringFromMap(String type ,HashMap<String, String> paramMap) throws MessegeFeedException{
		String value = paramMap.get(type);;
		if (value == null || value.equals("")) {
			throw new MessegeFeedException(Protocol.errormsg(Protocol.SENDORDED_FIELD, Protocol.ERROR_IN_FIELD + type));
		}
		return value;
	}
	
	
	private static Operation getOperationFromMap(HashMap<String, String> paramMap) throws MessegeFeedException {
		Operation type = null;
		String strType = getStringFromMap(MessegeParser.MsgFields.ORDERTYPE.name(), paramMap);
		switch (strType) {
		case "1":
			type = Operation.BUY;
			break;
		case "2":
			type = Operation.SELL;
			break;
		default:
			throw new MessegeFeedException(Protocol.errormsg(Protocol.SENDORDED_FIELD, Protocol.ERROR_IN_FIELD + MessegeParser.MsgFields.ORDERTYPE.name()));
		}
		return type;
	}

	private static Price getPriseFromMap(HashMap<String, String> paramMap) throws MessegeFeedException {
		boolean market = false;
		String strMarket = getStringFromMap(MessegeParser.MsgFields.MARKET.name(), paramMap);
		switch (strMarket) {
		case "1":
			market = true;
			break;
		case "2":
			market = false;
			break;
		default:
			throw new MessegeFeedException(Protocol.errormsg(Protocol.SENDORDED_FIELD, Protocol.ERROR_IN_FIELD + MessegeParser.MsgFields.MARKET.name()));
		}
		
		if (market) {
			return new Price();
		}
		
		String strPrise = getStringFromMap(MessegeParser.MsgFields.PRICE.name(), paramMap);
		BigDecimal price = new BigDecimal(strPrise); 
		return new Price(price);
	}
}
